package com.walterweimod.auxiliary_attributes.event;

import com.walterweimod.auxiliary_attributes.attribute.ModAttributes;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraftforge.event.entity.living.LivingHealEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.Objects;

import static com.walterweimod.auxiliary_attributes.util.ModUtils.*;

@Mod.EventBusSubscriber
public class FiremanEvent {
    @SubscribeEvent(priority = EventPriority.LOWEST)
    public static void onLivingEntityHealEnd(LivingHealEvent event){
        LivingEntity livingEntity = event.getEntityLiving();
        AttributeInstance max_heal_percent = livingEntity.getAttribute(ModAttributes.MAX_HEAL_PERCENT.get());

        if (Objects.nonNull(max_heal_percent)){
            float health = livingEntity.getHealth();
            float amount = Math.min(
                    health + event.getAmount(),
                    getLivingEntityMaxHealthPercent(
                            livingEntity, ((float) max_heal_percent.getValue())
                    )
            ) - health;

//            getLogger().info(ModUtils.hyphenName("fireman", 15));
//            getLogger().info("角色生命值为：{}", health);
//            getLogger().info("max_heal_percent属性值为：{}", max_heal_percent.getValue());
//            getLogger().info("角色最大可恢复生命值为：{}", getLivingEntityMaxHealthPercent(
//                    livingEntity, ((float) max_heal_percent.getValue())
//            ));
//            getLogger().info("纵火士最终回复量为：{}", amount);

            event.setAmount(amount);
        }
    }
}
